package appx.craft.droid.uc;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import appx.craft.droid.R;


public class MessageAdapter extends BaseAdapter{
	
	private ArrayList<String> mMessageArrayList;
	private LayoutInflater mLayoutInflater;
	static class ViewHolder{
		TextView mViewMessageContent;
		ImageView mImageViewData;
		Button mButtonAddItem;
	}
	
	public MessageAdapter(Context mContext){
		mMessageArrayList = new ArrayList<String>();
		mLayoutInflater = LayoutInflater.from(mContext);
	}
	
	public void addNewItem(String itemName){
		mMessageArrayList.add(itemName);
		notifyDataSetChanged();
	}
	
	public void addNewDataSet(final ArrayList<String> mNewDataSet){
		if(mNewDataSet != null){
			
			if(!mMessageArrayList.isEmpty()){
				mMessageArrayList.clear();
			}
			for(String mTempContent: mNewDataSet){
				mMessageArrayList.add(mTempContent);
			}
			notifyDataSetChanged();
		}
		
	}
	
	@Override
	public int getCount() {
		return mMessageArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return mMessageArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mHolderView;
		if(convertView == null){
			convertView = mLayoutInflater.inflate(R.layout.listview_addnew_item,parent,false);
			mHolderView = new ViewHolder();
			
			mHolderView.mViewMessageContent = (TextView)convertView.findViewById(R.id.textview_itemName);
			mHolderView.mImageViewData = (ImageView)convertView.findViewById(R.id.imageviewdata);
			mHolderView.mButtonAddItem = (Button)convertView.findViewById(R.id.btn_new_item_add);
			
			convertView.setTag(mHolderView);
			
		}else{
			
			mHolderView = (ViewHolder)convertView.getTag();
		}
		
		if(mMessageArrayList.size() > 0){
			
			if(position == 0){
				mHolderView.mButtonAddItem.setVisibility(View.VISIBLE);
				mHolderView.mImageViewData.setVisibility(View.GONE);
				
			}else{
				mHolderView.mButtonAddItem.setVisibility(View.GONE);
				mHolderView.mImageViewData.setVisibility(View.VISIBLE);
			}
			mHolderView.mViewMessageContent.setText(mMessageArrayList.get(position));
			
		}
		
		return convertView;
	}
	
	
}
